package com.talios.jira.browser;

import com.intellij.openapi.diagnostic.Logger;
import com.talios.jira.browser.signals.JiraServerDetails;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 22/01/2004
 * Time: 02:19:39
 * To change this template use File | Settings | File Templates.
 *
 * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
 * @jira INTPLUG-119 Stop working on Issue fails
 */
public class JiraItem {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private Integer keyValue;
    private String keyName;
    private String key;
    private String title;
    private String type;
    private String priority;
    private String status;
    private String resolution;
    private String assignee;
    private String reporter;
    private Date created;
    private Date updated;
    private Date dueDate;
    private String description;
    private String component;
    private String version;
    private String environment;

    private String fixVersion;

    private boolean beingWorkedOn;

    private JiraProjectDetails project;
//    private JiraServerDetails server;

    public JiraItem(JiraProjectDetails project) {
        this.project = project;
//        this.server = project.getServer();
    }

    public JiraItem(JiraServerDetails server) {
        this.project = new JiraProjectDetails(server);
//        this.server = server;
    }

    public JiraServerDetails getServer() {
        return project != null ? project.getServer() : null;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
        keyValue = Integer.valueOf(key.substring(key.indexOf("-") + 1));
        keyName = key.substring(0, key.indexOf("-"));
    }

    public Integer getKeyValue() {
        return keyValue;
    }

    public String getKeyName() {
        return keyName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getResolution() {
        return resolution;
    }

    public void setResolution(String resolution) {
        this.resolution = resolution;
    }

    public String getAssignee() {
        return assignee;
    }

    public void setAssignee(String assignee) {
        this.assignee = assignee;
    }

    public String getReporter() {
        return reporter;
    }

    public void setReporter(String reporter) {
        this.reporter = reporter;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getFixVersion() {
        return fixVersion;
    }

    public void setFixVersion(String fixVersion) {
        this.fixVersion = fixVersion;
    }

    public JiraProjectDetails getProject() {
        return project;
    }

    /**
     * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
     */
    public boolean isBeingWorkedOn() {
        return beingWorkedOn;
    }

    /**
     * Send's a request to the JIRA server to start working on an issue, this is a hack and would be much nicer
     * if it was an method via the OpenAPI.
     *
     * @jira INTPLUG-74 If Issue is already being worked on an exception is thrown
     * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
     */
    public void startWorkingOnIssue() {

        try {
            String baseUrl = project.getServer().getBaseurl()
                    + "/secure/WorkflowUIDispatcher.jspa?key=" + getKey() + "&action=201";

            baseUrl = baseUrl + project.getServer().getServerURIAttributes();

            URL url = new URL(baseUrl);
            url.getContent();

            beingWorkedOn = true;

        } catch (MalformedURLException e) {
            LOGGER.debug("Unable to start working on issue " + getKey() + ": " + e.getMessage());
        } catch (IOException e) {
            LOGGER.debug("Unable to start working on issue " + getKey() + ": " + e.getMessage());
        }

    }

    /**
     * @jira INTPLUG-39 Classes should also get annotated when working on an issue
     * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
     * @jira INTPLUG-119 Stop working on Issue fails
     */
    public void stopWorkingOnIssue() {
        try {
            String baseUrl = project.getServer().getBaseurl()
                    + "/secure/WorkflowUIDispatcher.jspa?key=" + getKey() + "&action=301";

            baseUrl = baseUrl + project.getServer().getServerURIAttributes();

            URL url = new URL(baseUrl);
            url.getContent();

            beingWorkedOn = false;

        } catch (FileNotFoundException e) {
            LOGGER.debug("Unable to start working on issue " + getKey() + ": " + e.getMessage());
            beingWorkedOn = false;
        } catch (MalformedURLException e) {
            LOGGER.error("Unable to stop working on issue: " + getKey());
        } catch (IOException e) {
            LOGGER.error("Unable to stop working on issue: " + getKey(), e);
        }

    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof JiraItem)) return false;

        final JiraItem jiraItem = (JiraItem) o;

        return key.equals(jiraItem.key);

    }

    public int hashCode() {
        return key.hashCode();
    }

    public String toString() {
        return getKey() + ' ' + getTitle();
    }

    public String getEnvironment() {
        return environment;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }
}
